package com.bdcom.autotest.ui.snp;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.springframework.util.CollectionUtils;

import com.bdcom.autotest.interaction.socket.DataUtil;
import com.bdcom.autotest.interaction.socket.DevSocket;
import com.bdcom.autotest.interaction.socket.bean.BasePkg;
import com.bdcom.autotest.interaction.step.StepThread;

public class MSSwitchTest extends AbsPanel {
	
	private StepThread mainrun;
	public ArrayList<String> passIndex = new ArrayList();
	public LinkedHashMap<String , JLabel> numToLabel = new LinkedHashMap<String, JLabel>();
	public MSSwitchTest(StepThread mainrun) {
		this.mainrun = mainrun;
//		this.slotCount = Integer.valueOf(mainrun.getShareVar(mainrun.key_card_cnt));
		this.setBackground(Color.WHITE);
		this.setPreferredSize(new Dimension( 0 , 250 ));
		this.setLayout( new FlowLayout(FlowLayout.LEFT ) );
		Font font2 = new Font("Dialog",Font.PLAIN, 14);
		
		for(   int i=0; i<2  ;i++  ) {
			String indexStr = ""+(i+1);
			JLabel jLabel = new JLabel("主控"+indexStr);
			jLabel.setFont(font2);
			byte[] a = DataUtil.int2Bytes(i);
			int b = i;
			JButton jbutton2 = new JButton("开始检测");
			jbutton2.addActionListener( new ActionListener() {
				public void actionPerformed(ActionEvent e){
					if(testMSU(b)){
						jbutton2.setEnabled(false);
					}
					
				}
			}
			);
			jbutton2.setFont(font2);
			JLabel jLabel2 = new JLabel("待测试");
			jLabel2.setFont(font2);
			this.add(jLabel);
			this.add(Box.createHorizontalStrut(10) );
			this.add(jbutton2);
			this.add(jLabel2);
			numToLabel.put(indexStr, jLabel2);
		}
		
	}
	public int getTest(){
		return 1;
	}


	public void setLabelValue(String indexStr,String result){
		JLabel label = numToLabel.get(indexStr);
		label.setText(result);
	}
	
	public boolean testMSU(int i){
		String indexStr = ""+(i+1);
		DevSocket devSocket = mainrun.getDevSokcet();
		byte[] bytes = new byte[6];
		bytes[0] = (byte)0x9E;
		bytes[1] = (byte)0x05;
		bytes[2] = (byte)0x00;
		bytes[3] = (byte)0x02;
		if(i==0){
			bytes[4] = (byte)0x01;//主控
		}else{
			bytes[4] = (byte)0x02;//主控
		}
	
		bytes[5] = (byte)0xAE;
		devSocket.writeMsg(bytes);
		byte cmd = (byte)0x05;
		BasePkg pkg = devSocket.pollAMsg(cmd);
		while(pkg==null){
			pkg = devSocket.pollAMsg(cmd);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		};
		byte[] data = pkg.data;
		if((data[0]==(byte)0x01&&i==0) ||(data[0]==(byte)0x02&&i==1) ){
			if(!passIndex.contains(indexStr)){
				passIndex.add(indexStr);
			}
			setLabelValue(indexStr,"通过");
			return true;
		}else{
			setLabelValue(indexStr,"不通过");
			return false;
		}
	}
	
	public boolean getTotalResult(){
		boolean result = false;
		while(!result){
			if(passIndex.size() == 2){
				result = true;
				return result;
			}
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}
}
